package com.kha.listone.screen.compound;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import com.kha.listone.ListOne;
import com.kha.listone.common.CompoundScreen;
import com.kha.listone.common.model.TaskModel;
import com.kha.listone.util.Commands;
import com.kha.listone.util.Globals;
import com.kha.listone.util.ImageLoader;
import com.kha.listone.util.operation.Operation;
import com.kha.listone.util.xml.TasksXMLParser;

public class TaskScreen extends CompoundScreen {

	public TaskScreen(ListOne midlet, String title, int type) {
		super(midlet, title, type);
	}

	protected void initScreen() {	

		// enqueueOperation.enqueue(new TasksXMLGenerator());
		enqueueOperation.enqueue(new TasksXMLParser());
		// enqueueOperation.enqueue(new SettingsXMLGenerator());
		enqueueOperation.enqueue(new RenderTask(this));
		enqueueOperation.start();

		this.addCommand(Commands.ADD);

		this.setSelectCommand(List.SELECT_COMMAND);

		this.addCommand(Commands.OPT_DETAIL);
		this.addCommand(Commands.OPT_ADD);
		this.addCommand(Commands.OPT_EDIT);
		this.addCommand(Commands.OPT_REMOVE);

		super.initScreen();
	}

	public void commandAction(Command c, Displayable d) {

		if (c == List.SELECT_COMMAND || c == Commands.OPT_DETAIL) {
			// To Detail Screen
			// Alert alert = new Alert("ListOne",
			// ((TaskModel) Globals.taskList.elementAt(this
			// .getSelectedIndex())).getDeadlineDate().toString(),
			// ImageLoader.load(ImageLoader.PICTURE), AlertType.INFO);
			//
			// mainMIDlet.showDisplay(alert);

			mainMIDlet.next(mainMIDlet
					.getDetailScreen((TaskModel) Globals.taskList
							.elementAt(this.getSelectedIndex())));
		}

		if (c == Commands.OPT_ADD || c == Commands.ADD) {
			mainMIDlet.next(mainMIDlet.getAddTaskScreen());
		}

		if (c == Commands.OPT_REMOVE) {

		}

		if (c == Commands.OPT_EDIT) {

		}

		super.commandAction(c, d);
	}

	private class RenderTask implements Operation {

		private TaskScreen screen;

		public RenderTask(TaskScreen screen) {
			this.screen = screen;
		}

		public void execute() {
			synchronized (screen) {

				for (int i = 0; i < Globals.taskList.size(); i++) {
					TaskModel task = (TaskModel) Globals.taskList.elementAt(i);

					if (task.getRecurrenceStatus() != TaskModel.RECURRENCE_ONCE) {
						screen.append(task.getTaskName(),
								ImageLoader.load(ImageLoader.TASK_RECURRENCE));
					} else if (task.getDone() == TaskModel.DONE) {
						screen.append(task.getTaskName(),
								ImageLoader.load(ImageLoader.TASK_DONE));
					} else {
						screen.append(task.getTaskName(), null);
					}

					// if (Globals
					// .isDayGreater(task.getDeadlineDate(), new Date())) {
					//
					// choiceActive.append(task.getTaskName(), null);
					//
					// }
					//
					// if (Globals.isTodayTask(task.getDueDate())) {
					//
					// choiceToday.append(task.getTaskName(), null);
					//
					// }
					//
					// if (task.getDone() == TaskModel.DONE) {
					// choiceDone.append(task.getTaskName(), null);
					// }

				}

			}
		}
	}

	public void refresh() {
		this.deleteAll();

		// enqueueOperation.enqueue(new TasksXMLGenerator());
		enqueueOperation.enqueue(new TasksXMLParser());
		// enqueueOperation.enqueue(new SettingsXMLGenerator());
		enqueueOperation.enqueue(new RenderTask(this));
		enqueueOperation.restart();
		super.refresh();
	}

}
